package com.jy.rhin.application.cda;

import com.jy.api.hospital.HospitalAPI;
import com.jy.api.hospital.dto.HospitalDTO;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.framework.base.utils.Converter;
import com.jy.rhin.application.cda.enums.HealthDictionaryEnum;
import com.jy.rhin.domain.cda.CDACreator;
import com.jy.rhin.infra.cda.CDAException;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import com.jy.rhin.infra.cda.service.YuFangJZService;
import com.jy.rhin.infra.cda.util.ZhiJiUtils;
import com.jy.rhin.infra.cda.yfjz.YuFangJZBLFYVo;
import com.jy.rhin.infra.cda.yfjz.YuFangJZJLVo;
import com.jy.rhin.infra.cda.yfjz.YuFangJZKVo;
import com.jy.rhin.model.ClinicalDocumentData;
import com.jy.rhin.model.CodeObject;
import com.jy.rhin.model.YuFangJZ;
import com.jy.rhin.model.body.jiwangshi.XinShengEJWS;
import com.jy.rhin.model.body.shoushucaozuo.JieZhongXX;
import com.jy.rhin.model.body.yichangfanying.JieZhongYCFY;
import com.jy.rhin.model.header.*;
import com.jy.rhin.utils.CDAUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class YuFangJZApplication {

    @Autowired
    private CDACreator cdaCreator;
    @Autowired
    private YuFangJZService yuFangJZService;
    /*@Autowired
    private DataMigrationAPI dataMigrationAPI;*/
    @Autowired
    private ShuYuService shuYuService;
    @Resource
    private HospitalAPI hospitalAPI;

    public String create(String yuFangJZKBH, String yeWuJGDM) {
        return cdaCreator.create(createYuFangJZ(yuFangJZKBH, yeWuJGDM), "09yufangjiezhong.ftl");
    }

    public ClinicalDocumentData createYuFangJZ(String yuFangJZKBH, String yeWuJGDM) {
        YuFangJZKVo yuFangJZKVo = yuFangJZService.getYuFangJZK(yuFangJZKBH, yeWuJGDM);
        if (Objects.isNull(yuFangJZKVo)) {
            throw new CDAException("主业务数据不存在", null);
        }
        HospitalDTO hospital = hospitalAPI.info(yeWuJGDM).getData();
//        HospitalDTO hospital = dataMigrationAPI.getByHospitalId(yeWuJGDM).getData();
        if (Objects.isNull(hospital)) {
            throw new CDAException("机构数据不存在", null);
        }

        List<YuFangJZJLVo> yuFangJZJLVos = yuFangJZService.getYuFangJZJL(yuFangJZKBH, yeWuJGDM);
        List<YuFangJZBLFYVo> yuFangJZBLFYVos = yuFangJZService.getYuFangJZBLFY(yuFangJZKBH, yeWuJGDM);

        shuYuZhiYuQuery(yuFangJZKVo, yuFangJZJLVos, yuFangJZBLFYVos);

        YuFangJZ docData = new YuFangJZ();
        setClinicalDocumentData(docData, yuFangJZKVo, hospital);

        XinShengEJWS xinShengEJWS = new XinShengEJWS();
        xinShengEJWS.setJieZhongYCFYS(yuFangJZKVo.getYIMIAOYCFYS());
        xinShengEJWS.setJieZhongJJ(Converter.toString(yuFangJZKVo.getJIEZHONGJJMC()));
        xinShengEJWS.setChuanRanBS("-");
        docData.setXinShengEJWS(xinShengEJWS);
        if (!CollectionUtils.isEmpty(yuFangJZJLVos)) {
            List<JieZhongXX> jieZhongXXs = new ArrayList<>();
            for (YuFangJZJLVo yuFangJZJLVo : yuFangJZJLVos) {
                HospitalDTO hospital2 = hospitalAPI.info(yuFangJZJLVo.getYEWUJGDM()).getData();
//                HospitalDTO hospital2 = dataMigrationAPI.getByHospitalId(yuFangJZJLVo.getYEWUJGDM()).getData();
                if (Objects.isNull(hospital2)) {
                    throw new CDAException("接诊记录机构数据不存在", null);
                }

                JieZhongXX jieZhongXX = new JieZhongXX();
                jieZhongXX.setJieZhongSJ(CDAUtil.toD8(yuFangJZJLVo.getJIEZHONGRQ()));
                jieZhongXX.setJieZhongJC(Converter.toString(yuFangJZJLVo.getJIEZHONGJC()));
                jieZhongXX.setJieZhongBW(yuFangJZJLVo.getJIEZHONGBW());
                jieZhongXX.setJieZhongYSDM(yuFangJZJLVo.getJIEZHONGYSXM());
                jieZhongXX.setJieZhongYSXM(yuFangJZJLVo.getJIEZHONGYSXM());
                jieZhongXX.setJieZhongJGID(CDAUtil.toJGID10(hospital2.getOutId()));
                jieZhongXX.setJieZhongJGMC(yuFangJZJLVo.getJIEZHONGDWMC());
                jieZhongXX.setYiMiaoPH(yuFangJZJLVo.getYIMIAOPH());
                jieZhongXX.setYiMiaoMC(new CodeObject(yuFangJZJLVo.getYIMIAODM(), yuFangJZJLVo.getYIMIAOMC()));
                jieZhongXX.setYiMiaoSCCSBH(yuFangJZJLVo.getSHENGCHANQYDM());
                jieZhongXX.setYiMiaoSCCSMC(yuFangJZJLVo.getSHENGCHANQYDM());
                jieZhongXXs.add(jieZhongXX);
            }
            docData.setJieZhongXX(jieZhongXXs);
        }

        if (!CollectionUtils.isEmpty(yuFangJZBLFYVos)) {
            List<JieZhongYCFY> jieZhongYCFYs = new ArrayList<>();
            for (YuFangJZBLFYVo yuFangJZBLFYVo : yuFangJZBLFYVos) {
                JieZhongYCFY jieZhongYCFY = new JieZhongYCFY();
                jieZhongYCFY.setKeYiYMDM(new CodeObject(yuFangJZBLFYVo.getKEYIYMMC(), yuFangJZBLFYVo.getKEYIYMMC2()));
                jieZhongYCFY.setZhenDuanDM(new CodeObject(yuFangJZBLFYVo.getZHENDUANDM(), yuFangJZBLFYVo.getZHENDUANMC()));
                jieZhongYCFY.setChuLiJG(yuFangJZBLFYVo.getCHULIJG());
                jieZhongYCFY.setFanYingRQ(CDAUtil.toD8(yuFangJZBLFYVo.getFASHENGRQ()));
                jieZhongYCFYs.add(jieZhongYCFY);
            }
            docData.setJieZhongYCFY(jieZhongYCFYs);
        }

        return docData;
    }

    private void shuYuZhiYuQuery(YuFangJZKVo yuFangJZKVo, List<YuFangJZJLVo> yuFangJZJLVos, List<YuFangJZBLFYVo> yuFangJZBLFYVos) {
        List<ZhiYuMXCXDto> zhiJiBSFList = new ArrayList<>();
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GB_T_2261_1.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(yuFangJZKVo.getXINGBIE()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.GB_T_4761.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(yuFangJZKVo.getGUANXIDM()))));
        ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_JIEZHONGJJ.getCode(), new HashSet<String>(Arrays.asList(Converter.toString(yuFangJZKVo.getJIEZHONGJJDM()))));

        if (!CollectionUtils.isEmpty(yuFangJZBLFYVos)) {
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CVX_YIMIAODM.getCode(), yuFangJZBLFYVos.stream().map(YuFangJZBLFYVo::getKEYIYMMC).collect(Collectors.toSet()));
            ZhiJiUtils.addValueItem(zhiJiBSFList, HealthDictionaryEnum.CV05_01_032.getCode(), yuFangJZBLFYVos.stream().map(YuFangJZBLFYVo::getZHENDUANDM).collect(Collectors.toSet()));
        }

        Map<String, List<DaiMaDto>> valueByCode = shuYuService.getValueByCode(zhiJiBSFList.toArray(new ZhiYuMXCXDto[0]));

        yuFangJZKVo.setXINGBIEMC(ZhiJiUtils.getValue(HealthDictionaryEnum.GB_T_2261_1.getCode(), Converter.toString(yuFangJZKVo.getXINGBIE()), valueByCode));
        yuFangJZKVo.setGUANXIMC(ZhiJiUtils.getValue(HealthDictionaryEnum.GB_T_4761.getCode(), Converter.toString(yuFangJZKVo.getGUANXIDM()), valueByCode));
        yuFangJZKVo.setJIEZHONGJJMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_JIEZHONGJJ.getCode(), Converter.toString(yuFangJZKVo.getJIEZHONGJJDM()), valueByCode));
        if (!CollectionUtils.isEmpty(yuFangJZBLFYVos)) {
            yuFangJZBLFYVos.forEach(info -> {
                info.setKEYIYMMC2(ZhiJiUtils.getValue(HealthDictionaryEnum.CVX_YIMIAODM.getCode(), info.getKEYIYMMC(), valueByCode));
                info.setZHENDUANMC(ZhiJiUtils.getValue(HealthDictionaryEnum.CV05_01_032.getCode(), info.getZHENDUANDM(), valueByCode));
            });
        }
    }

    ClinicalDocumentData setClinicalDocumentData(ClinicalDocumentData clinicalDocumentData, YuFangJZKVo yuFangJZKVo, HospitalDTO hospital) {
        clinicalDocumentData.setEffectiveTime(CDAUtil.toD15(yuFangJZKVo.getGENGXINSJ()));
        clinicalDocumentData.setPatientRole(createPatientRole(yuFangJZKVo));
        clinicalDocumentData.setAuthor(createAuthor(yuFangJZKVo, hospital));
        clinicalDocumentData.setCustodian(createCustodian(hospital));
        clinicalDocumentData.setId(String.valueOf(yuFangJZKVo.getID()));
        return clinicalDocumentData;
    }

    Author createAuthor(YuFangJZKVo yuFangJZKVo, HospitalDTO hospital) {
        Author author = new Author();
        author.setTime(CDAUtil.toD15(yuFangJZKVo.getKAPIANCJSJ()));
        author.setId("-");
        author.setName("无");
        author.setOrganizationId(CDAUtil.toJGID10(hospital.getOutId()));
        author.setOrganizationName(hospital.getName());
        author.setOrganizationTelecom(hospital.getDanWeiDH());
        author.setOrganizationAddr(hospital.getAddress());
        return author;
    }

    Custodian createCustodian(HospitalDTO hospital) {
        Custodian custodian = new Custodian();
        custodian.setId(CDAUtil.toJGID10(hospital.getOutId()));
        custodian.setName(hospital.getName());
        custodian.setTelecom(hospital.getDanWeiDH());
        custodian.setAddr(hospital.getAddress());
        return custodian;
    }

    PatientRole createPatientRole(YuFangJZKVo yuFangJZKVo) {
        PatientRole patientRole = new PatientRole();
        patientRole.setId(CDAUtil.formatNumber(yuFangJZKVo.getYUFANGJZKBH()));
        patientRole.setTelecom(yuFangJZKVo.getDIANHUA());
        patientRole.setAddr(createAddr(yuFangJZKVo));
        patientRole.setPatient(createPatient(yuFangJZKVo));
        return patientRole;
    }

    Addr createAddr(YuFangJZKVo yuFangJZKVo) {
        Addr addr = new Addr();
        addr.setState(yuFangJZKVo.getXIANZHUZSFMC());
        addr.setCity(yuFangJZKVo.getXIANZHUZSMC());
        addr.setCounty(yuFangJZKVo.getXIANZHUZXQMC());
        addr.setTownship(yuFangJZKVo.getXIANZHUZSQMC());
        addr.setStreetName(yuFangJZKVo.getXIANZHUZXZMC());
        addr.setHouseNumber(yuFangJZKVo.getXIANZHUZQTDZ());
        return addr;
    }

    Patient createPatient(YuFangJZKVo yuFangJZKVo) {
        Patient patient = new Patient();
        patient.setId(yuFangJZKVo.getZHENGJIANHM());
        patient.setName(yuFangJZKVo.getXINGMING());
        patient.setAdministrativeGender(new CodeObject(Converter.toString(yuFangJZKVo.getXINGBIE()), yuFangJZKVo.getXINGBIEMC()));
        patient.setBirthTime(CDAUtil.toD8(yuFangJZKVo.getCHUSHENGRQ()));
        patient.setGuardian(createGuardian(yuFangJZKVo));
        patient.setHouseholdAddr(createHouseholdAddr(yuFangJZKVo));
        return patient;
    }

    Guardian createGuardian(YuFangJZKVo yuFangJZKVo) {
        Guardian guardian = new Guardian();
        guardian.setCode(new CodeObject(Converter.toString(yuFangJZKVo.getGUANXIDM()), yuFangJZKVo.getGUANXIMC()));
        guardian.setTelecom(yuFangJZKVo.getJIANHURENDH());
        guardian.setName(yuFangJZKVo.getJIANHURXM());
        return guardian;
    }

    Addr createHouseholdAddr(YuFangJZKVo yuFangJZKVo) {
        Addr addr = new Addr();
        addr.setState(yuFangJZKVo.getHUJISFMC());
        addr.setCity(yuFangJZKVo.getHUJISMC());
        addr.setCounty(yuFangJZKVo.getHUJIXQMC());
        addr.setTownship(yuFangJZKVo.getHUJISQMC());
        addr.setStreetName(yuFangJZKVo.getHUJIXZMC());
        addr.setHouseNumber(yuFangJZKVo.getHUJIQTDZ());
        return addr;
    }

}
